{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Conditionals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A conditional is a construct that allows you to branch your code based on conditions being met (or not)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is achieved using **if**, **elif** and **else** or the **ternary operator** (aka conditional expression)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a < 3\n"
     ]
    }
   ],
   "source": [
    "a = 2\n",
    "if a < 3:\n",
    "    print('a < 3')\n",
    "else:\n",
    "    print('a >= 3')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**if** statements can be nested:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a >= 10\n"
     ]
    }
   ],
   "source": [
    "a = 15\n",
    "\n",
    "if a < 5:\n",
    "    print('a < 5')\n",
    "else:\n",
    "    if a < 10:\n",
    "        print('5 <= a < 10')\n",
    "    else:\n",
    "        print('a >= 10')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But the **elif** statement provides far better readability:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a >= 10\n"
     ]
    }
   ],
   "source": [
    "a = 15\n",
    "if a < 5:\n",
    "    print('a < 5')\n",
    "elif a < 10:\n",
    "    print('5 <= a < 10')\n",
    "else:\n",
    "    print('a >= 10')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Python, **elif** is the closest you'll find to the switch/case statement available in some other languages."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python also provides a conditional expression (ternary operator):\n",
    "\n",
    "X if (condition) else Y\n",
    "\n",
    "returns (and evaluates) X if (condition) is True, otherwise returns (and evaluates) Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a < 10\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "res = 'a < 10' if a < 10 else 'a >= 10'\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a >= 10\n"
     ]
    }
   ],
   "source": [
    "a = 15\n",
    "res = 'a < 10' if a < 10 else 'a >= 10'\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that **X** and **Y** can be any expression, not just literal values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def say_hello():\n",
    "    print('Hello!')\n",
    "    \n",
    "def say_goodbye():\n",
    "    print('Goodbye!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello!\n"
     ]
    }
   ],
   "source": [
    "a = 5\n",
    "say_hello() if a < 10 else say_goodbye()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Goodbye!\n"
     ]
    }
   ],
   "source": [
    "a = 15\n",
    "say_hello() if a < 10 else say_goodbye()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
